//
//  ZYMinuteSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/7.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYMinuteSelectView.h"
#import "ZYViewUtils.h"
#import "ZYImageUtils.h"

@interface ZYMinuteSelectView()
@property(nonatomic,strong) UIView *bigBgView;
@property(nonatomic,strong) UIView *bgView;

/**
 增加
 */
@property(nonatomic,strong) UIButton *addBtn;

/**
 减少
 */
@property(nonatomic,strong) UIButton *reduceBtn;

/**
 分钟数
 */
@property(nonatomic,strong) UILabel *minuteLabel;


/**
 按钮背景
 */
@property(nonatomic,strong) UIView *buttonBgView;
@property(nonatomic,strong) UIButton *confirmButton;
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYMinuteSelectView
typedef NS_ENUM(NSInteger,ZYMinuteSelectViewButtonType){
    /**加*/
    ZYMinuteSelectViewButtonTypeAdd = 0,
    /**减*/
    ZYMinuteSelectViewButtonTypeReduce,
};
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _bigBgView = [[UIView alloc] initWithFrame:self.bounds];
    _bigBgView.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self addSubview:_bigBgView];
    
    CGFloat width = self.width * 0.92;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width * 0.53)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    _bgView.centerX = self.centerX;
    _bgView.centerY = self.centerY - _bgView.height / 4;
    
    CGFloat viewWidth = _bgView.width * 0.74;
    _addBtn = [self setupButtonWithTitle:@"+"];
    _addBtn.tag = ZYMinuteSelectViewButtonTypeAdd;
    _reduceBtn = [self setupButtonWithTitle:@"-"];
    _reduceBtn.tag = ZYMinuteSelectViewButtonTypeReduce;
    _minuteLabel = [self setupLabel];
    
    //初始化按钮组背景
    _buttonBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, _addBtn.height)];
    _buttonBgView.layer.borderColor = kMainColor.CGColor;
    _buttonBgView.layer.borderWidth = 1;
    _buttonBgView.layer.cornerRadius = 5;
    _buttonBgView.layer.masksToBounds = YES;
    [_bgView addSubview:_buttonBgView];
    _buttonBgView.centerX = _bgView.width / 2;
    _buttonBgView.centerY = _bgView.height * 0.36;
    
    [_buttonBgView addSubview:_addBtn];
    [_buttonBgView addSubview:_reduceBtn];
    [_buttonBgView addSubview:_minuteLabel];
    _minuteLabel.size = CGSizeMake(viewWidth - _addBtn.width * 2, _addBtn.height);
    
    _minuteLabel.centerX = _buttonBgView.width / 2;
    _minuteLabel.centerY = _buttonBgView.height / 2;
    
    _reduceBtn.centerY = _minuteLabel.centerY;
    _reduceBtn.right = _minuteLabel.left ;
    
    _addBtn.centerY = _minuteLabel.centerY;
    _addBtn.left = _minuteLabel.right;
    
    //确认
    _confirmButton = [ZYViewUtils setupConfirmBtn];
    _confirmButton.size = CGSizeMake(width / 3, width * 0.12);
    [_bgView addSubview:_confirmButton];
    _confirmButton.centerX = _bgView.width / 2;
    _confirmButton.top = _buttonBgView.bottom + _buttonBgView.height * 0.6;
    [_confirmButton addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [_confirmButton setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE] forState:UIControlStateNormal];
    
    self.alpha = 0;
    self.bigBgView.hidden = YES;
    self.top = -self.height;
    self.backgroundColor = [UIColor clearColor];
    
    self.minute = 30;
}

-(UILabel *)setupLabel{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kLabelColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18];
    label.layer.borderWidth = 1;
    label.layer.borderColor = kMainColor.CGColor;
    return label;
}

-(UIButton *)setupButtonWithTitle:(NSString *)title{
    CGFloat width = _bgView.width * 0.2;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, 0.66 * width)];
    [button setTitleColor:kMainColor forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    UIFont *buttonFont = [UIFont systemFontOfSize:20];
    [button.titleLabel setFont:buttonFont];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:button.bounds] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}


#pragma mark public
-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

#pragma mark - Event

-(void)doButtonClick:(UIButton *)button{
    if(button.tag == ZYMinuteSelectViewButtonTypeReduce){
        if(_minute == 0){
            return;
        }
        self.minute --;
    }else{
        if(_minute == 600){
            return;
        }
        self.minute ++;
    }
}

-(void)doConfirm:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(minuteSelectView:didSelectWithMinute:)]){
        [_delegate minuteSelectView:self didSelectWithMinute:_minute];
    }
    [self close];
}

-(void)setMinute:(int)minute{
    _minute = minute;
    _minuteLabel.text = [NSString stringWithFormat:@"%d%@",minute,[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_QUICK_SETUP_MINUTE_LABEL]];
}

@end
